
module windump.xml.node;


class XmlEntities
{
	static char[][char] m_entities;

    public static this()
	{
		m_entities['&'] = "&amp;";
		m_entities['<'] = "&lt;";
		m_entities['>'] = "&gt;";
		m_entities['\''] = "&apos;";
		m_entities['\"'] = "&quot;";
		m_entities['\0'] = " ";
	}

    public static char[] encode(char[] text)
    {
        char[] ret;
        foreach(char c; text)
        {
			if(c in m_entities)
			{
				ret ~= m_entities[c];
			}
			else ret ~= c;
        }
        return ret;
    }

}


abstract class XmlNode
{
    protected XmlNode    m_parent = null;
    protected XmlNode[]  m_children; 
    protected XmlNode    m_nextSibling = null;
    protected char[]  m_name;

    public this(char[] name)
    {
        m_name = name;
    }    

    public XmlNode appendChild(XmlNode newChild)
    {
        m_children ~= newChild;   
        return newChild;
    }

/*    Node insert()
    {
    }*/

    public XmlNode[] children()
    {
        return m_children;
    }

    public XmlNode parent()
    {
        assert(parent);
        return parent;
    }

    public XmlNode firstChild()
    {
        if(m_children.length <= 0)return null;
        return m_children[0];
    }

    public XmlNode lastChild()
    {
        if(m_children.length <= 0)return null;
        return m_children[length - 1];
    }

    public XmlNode nextSibling()
    {
        return m_nextSibling;
    }

    public bool hasChildren()
    {
        return m_children.length > 0;
    }

    public char[] name() 
    {
        return m_name; 
    }

    public void name(char[] name) 
    {
        m_name = name;
    }

    public abstract char[] outerXml();

    public abstract char[] innerXml();

}
